button for canvas toolbar which enables creating a pdf of the figure#443
button for canvas toolbar which enables creating a pdf of the figure#443jkochNU wants to merge 2 commits intomatplotlib:mainfrom
Conversation
for more information, see https://pre-commit.ci
ianhi
left a comment
There was a problem hiding this comment.
This is a good start! Glad that you managed all the interconnections between python and typescript.
That said I don't think that the correct solution is to limit ourselves to pdfs here. Instead we could have the button mean "Save according to rcparams" and a second button (probably with a different icon) that would do the current behavior of downloading.
For now just adding a new button for saving with rcparams is a good step I think. inline comments should show how to accomplish that.
| self.figure.savefig(buf, format='pdf', dpi='figure') | ||
| self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()]) |
There was a problem hiding this comment.
Changing this a little bit will allow generalizing to more than just PDFs.
- Remove the arguments to savefig so that it follows the rcparams
- check the rcparams format and use that to construct the mimetype
- send that mimetype info along to the frontend
| self.figure.savefig(buf, format='pdf', dpi='figure') | |
| self.send({'data': '{"type": "makepdf"}'}, buffers=[buf.getbuffer()]) | |
| self.figure.savefig(buf) | |
| format = matplotlib.rcParams['savefig.format'] | |
| # some code intepreting the mimetype | |
| # e..g application/pdf, image/svg+xml, image/png etc | |
| self.send({'data': {"type": "download-fig", "format":format, "mimetype":mimetype}}, buffers=[buf.getbuffer()]) |
| handle_makepdf(msg: any, dataviews: any) { | ||
| const url_creator = window.URL || window.webkitURL; | ||
| const buffer = new Uint8Array(dataviews[0].buffer); | ||
| const blob = new Blob([buffer], { type: 'application/pdf' }); |
There was a problem hiding this comment.
With other comment extract the mimetype from the msg
| const image_url = url_creator.createObjectURL(blob); | ||
| window.open(image_url, '_blank'); |
There was a problem hiding this comment.
I'm pretty sure that we need to revoke the object url after downloading
|
@jkochNU heads up that the pre-commit bot push a commit with styling changes. You can either |
@ianhi Regarding your suggested solution: we should keep in mind that |
As discussed with @ianhi, this is an attempt to (partially) address #138.
This would add an additional button to the toolbar associated with a
figure.canvas. Once clicked, a pdf is generated withsavefigand then handed over to the frontend inside a new tab.